//
//  HistoryViewController.m
//  HappyNews
//
//  Created by lanou on 16/1/11.
//  Copyright © 2016年 BPWBANP. All rights reserved.
//

#import "HistoryViewController.h"

#import "HistoryHeaderCollectionReusableView.h"

#import "HistoryCollectionViewCell.h"

#import "DCPicScrollView.h"

#import "DCWebImageManager.h"

#import "HNRequestManager.h"

#import "RecreationViewModel.h"

#define HistoryURL @"http://vcsp.ifeng.com/vcsp/appData/recommendGroupByTeamid.do?channelId=100384-0&showType=double&useType=iPhone&positionId=&adapterNo=6.11.1&isNotModified=0"
@interface HistoryViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic, strong) UICollectionView *HistoryCollection;

@property (nonatomic, strong) NSMutableArray *headerHistoryArr;

@property (nonatomic, strong) NSMutableArray *titleHistoryArr;

@property (nonatomic, strong) NSMutableArray *imageHistoryArr;

@property (nonatomic, strong) NSMutableArray *HistoryCellArr;

@property (nonatomic, strong) HistoryHeaderCollectionReusableView *header;


@end

@implementation HistoryViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self addHistoryConnection];
    
    [self setHistoryUpData];
    
    _titleHistoryArr = [NSMutableArray array];
    
    _imageHistoryArr = [NSMutableArray array];
    
    _headerHistoryArr = [NSMutableArray array];
    
    _HistoryCellArr = [NSMutableArray array];
}

- (void)setHistoryUpData
{
    [HNRequestManager requestWithUrlString:HistoryURL parDic:nil method:GET finish:^(NSData *data) {
        
        NSDictionary *HistoryDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        
        NSArray *HistoryArray = HistoryDic[@"header"];
        
        for (NSMutableDictionary *headerHistoryDic in HistoryArray) {
            
            RecreationViewModel *model = [[RecreationViewModel alloc]init];
            
            [model setValuesForKeysWithDictionary:headerHistoryDic];
            
            
            [_titleHistoryArr addObject:model.title];
            
            [_imageHistoryArr addObject:model.image];
            
            [_headerHistoryArr addObject:model];
        }
        
        
        
        
        // cell
        NSArray *bodyListHistoryArr = HistoryDic[@"bodyList"];
        
        for (NSMutableDictionary *HistoryOneDic in bodyListHistoryArr) {
            
            NSArray *videoListHistoryArr = HistoryOneDic[@"videoList"];
            
            for (NSMutableDictionary *HistoryTwoDic in videoListHistoryArr) {
                
                RecreationViewModel *model = [[RecreationViewModel alloc]init];
                
                [model setValuesForKeysWithDictionary:HistoryTwoDic];
                
                [_HistoryCellArr addObject:model];
            }
            
        }
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [_HistoryCollection reloadData];
        });
        
    } error:^(NSError *error) {
        
    }];
}

- (void)addHistoryConnection
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    
    layout.minimumInteritemSpacing = 10;
    
    layout.minimumLineSpacing = 10;
    
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    layout.itemSize = CGSizeMake((kScreenWidth - 30) / 2, kScreenHeight / 3.7);
    
    layout.headerReferenceSize = CGSizeMake(kScreenWidth, kScreenHeight / 3);
    
    _HistoryCollection = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 155) collectionViewLayout:layout];
    
    _HistoryCollection.delegate = self;
    
    _HistoryCollection.dataSource = self;
    
    _HistoryCollection.backgroundColor = [UIColor whiteColor];
    
    [_HistoryCollection registerClass:[HistoryCollectionViewCell class] forCellWithReuseIdentifier:@"HistoryCollectionViewCell"];
    
    [_HistoryCollection registerClass:[HistoryHeaderCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HistoryHeaderCollectionReusableView"];
    
    [self.view addSubview:_HistoryCollection];
    
    
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _HistoryCellArr.count;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    _header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HistoryHeaderCollectionReusableView" forIndexPath:indexPath];
    _header.titleHistoryArr = _titleHistoryArr;
    _header.imageHistoryArr = _imageHistoryArr;
    
    _header.headerHistoryArr = _headerHistoryArr;
    
    __weak NSArray *myheaderHistoryArr = _headerHistoryArr;
    
    __weak id myDelegate = _delegate;
    
    [_header.picView setImageViewDidTapAtIndex:^(NSInteger index) {
        
        
        if (myDelegate && [myDelegate respondsToSelector:@selector(addHistory:)]) {
            [myDelegate addHistory:myheaderHistoryArr[index]];
        }
        
    }];
    
    
    return _header;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    HistoryCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HistoryCollectionViewCell" forIndexPath:indexPath];
    
    cell.model = _HistoryCellArr[indexPath.row];
    
    return cell;
    
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (_delegate && [_delegate respondsToSelector:@selector(addHistory:)]) {
        [_delegate addHistory:_HistoryCellArr[indexPath.row]];
    }
    
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
